package week5;

import java.util.Arrays;

public class Ex41 {
    public static void main(String[] args) {
        int n = 7654321;
        int[] prime = new int[n];
        int c = 0;
        boolean[] isVisit = new boolean[n+1];
        for (int i = 2; i <= n; ++i){
            if (!isVisit[i])
                prime[++c] = i;
            for (int j = 1; j <= c && i * prime[j] <= n; ++j){
                isVisit[i*prime[j]] = true;
                if (i % prime[j] == 0)
                    break;
            }
        }
        char[] arr;
        for (int i = c; i > 0; --i){
            arr = String.valueOf(prime[i]).toCharArray();
            Arrays.sort(arr);
            if (String.valueOf(arr).equals("1234567")){
                System.out.println(prime[i]);
                break;
            }
            if (String.valueOf(arr).equals("1234")){
                System.out.println(prime[i]);
                break;
            }
        }
    }
}
